{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 8.25 \u521b\u5efa\u7f13\u5b58\u5b9e\u4f8b\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u95ee\u9898\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5728\u521b\u5efa\u4e00\u4e2a\u7c7b\u7684\u5bf9\u8c61\u65f6\uff0c\u5982\u679c\u4e4b\u524d\u4f7f\u7528\u540c\u6837\u53c2\u6570\u521b\u5efa\u8fc7\u8fd9\u4e2a\u5bf9\u8c61\uff0c \u4f60\u60f3\u8fd4\u56de\u5b83\u7684\u7f13\u5b58\u5f15\u7528\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u89e3\u51b3\u65b9\u6848\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u79cd\u901a\u5e38\u662f\u56e0\u4e3a\u4f60\u5e0c\u671b\u76f8\u540c\u53c2\u6570\u521b\u5efa\u7684\u5bf9\u8c61\u65f6\u5355\u4f8b\u7684\u3002\n\u5728\u5f88\u591a\u5e93\u4e2d\u90fd\u6709\u5b9e\u9645\u7684\u4f8b\u5b50\uff0c\u6bd4\u5982 logging \u6a21\u5757\uff0c\u4f7f\u7528\u76f8\u540c\u7684\u540d\u79f0\u521b\u5efa\u7684 logger \u5b9e\u4f8b\u6c38\u8fdc\u53ea\u6709\u4e00\u4e2a\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import logging\na = logging.getLogger('foo')\nb = logging.getLogger('bar')\na is b"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c = logging.getLogger('foo')\na is c"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e3a\u4e86\u8fbe\u5230\u8fd9\u6837\u7684\u6548\u679c\uff0c\u4f60\u9700\u8981\u4f7f\u7528\u4e00\u4e2a\u548c\u7c7b\u672c\u8eab\u5206\u5f00\u7684\u5de5\u5382\u51fd\u6570\uff0c\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# The class in question\nclass Spam:\n    def __init__(self, name):\n        self.name = name\n\n# Caching support\nimport weakref\n_spam_cache = weakref.WeakValueDictionary()\ndef get_spam(name):\n    if name not in _spam_cache:\n        s = Spam(name)\n        _spam_cache[name] = s\n    else:\n        s = _spam_cache[name]\n    return s"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7136\u540e\u505a\u4e00\u4e2a\u6d4b\u8bd5\uff0c\u4f60\u4f1a\u53d1\u73b0\u8ddf\u4e4b\u524d\u90a3\u4e2a\u65e5\u5fd7\u5bf9\u8c61\u7684\u521b\u5efa\u884c\u4e3a\u662f\u4e00\u81f4\u7684\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = get_spam('foo')\nb = get_spam('bar')\na is b"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "c = get_spam('foo')\na is c"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "### \u8ba8\u8bba\n"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7f16\u5199\u4e00\u4e2a\u5de5\u5382\u51fd\u6570\u6765\u4fee\u6539\u666e\u901a\u7684\u5b9e\u4f8b\u521b\u5efa\u884c\u4e3a\u901a\u5e38\u662f\u4e00\u4e2a\u6bd4\u8f83\u7b80\u5355\u7684\u65b9\u6cd5\u3002\n\u4f46\u662f\u6211\u4eec\u8fd8\u80fd\u5426\u627e\u5230\u66f4\u4f18\u96c5\u7684\u89e3\u51b3\u65b9\u6848\u5462\uff1f"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4f8b\u5982\uff0c\u4f60\u53ef\u80fd\u4f1a\u8003\u8651\u91cd\u65b0\u5b9a\u4e49\u7c7b\u7684 __new__() \u65b9\u6cd5\uff0c\u5c31\u50cf\u4e0b\u9762\u8fd9\u6837\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# Note: This code doesn't quite work\nimport weakref\n\nclass Spam:\n    _spam_cache = weakref.WeakValueDictionary()\n    def __new__(cls, name):\n        if name in cls._spam_cache:\n            return cls._spam_cache[name]\n        else:\n            self = super().__new__(cls)\n            cls._spam_cache[name] = self\n            return self\n    def __init__(self, name):\n        print('Initializing Spam')\n        self.name = name"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u521d\u770b\u8d77\u6765\u597d\u50cf\u53ef\u4ee5\u8fbe\u5230\u9884\u671f\u6548\u679c\uff0c\u4f46\u662f\u95ee\u9898\u662f __init__() \u6bcf\u6b21\u90fd\u4f1a\u88ab\u8c03\u7528\uff0c\u4e0d\u7ba1\u8fd9\u4e2a\u5b9e\u4f8b\u662f\u5426\u88ab\u7f13\u5b58\u4e86\u3002\u4f8b\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s = Spam('Dave')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "t = Spam('Dave')"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "s is t"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u4e2a\u6216\u8bb8\u4e0d\u662f\u4f60\u60f3\u8981\u7684\u6548\u679c\uff0c\u56e0\u6b64\u8fd9\u79cd\u65b9\u6cd5\u5e76\u4e0d\u53ef\u53d6\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u4e0a\u9762\u6211\u4eec\u4f7f\u7528\u5230\u4e86\u5f31\u5f15\u7528\u8ba1\u6570\uff0c\u5bf9\u4e8e\u5783\u573e\u56de\u6536\u6765\u8bb2\u662f\u5f88\u6709\u5e2e\u52a9\u7684\uff0c\u5173\u4e8e\u8fd9\u4e2a\u6211\u4eec\u57288.23\u5c0f\u8282\u5df2\u7ecf\u8bb2\u8fc7\u4e86\u3002\n\u5f53\u6211\u4eec\u4fdd\u6301\u5b9e\u4f8b\u7f13\u5b58\u65f6\uff0c\u4f60\u53ef\u80fd\u53ea\u60f3\u5728\u7a0b\u5e8f\u4e2d\u4f7f\u7528\u5230\u5b83\u4eec\u65f6\u624d\u4fdd\u5b58\u3002\n\u4e00\u4e2a WeakValueDictionary \u5b9e\u4f8b\u53ea\u4f1a\u4fdd\u5b58\u90a3\u4e9b\u5728\u5176\u5b83\u5730\u65b9\u8fd8\u5728\u88ab\u4f7f\u7528\u7684\u5b9e\u4f8b\u3002\n\u5426\u5219\u7684\u8bdd\uff0c\u53ea\u8981\u5b9e\u4f8b\u4e0d\u518d\u88ab\u4f7f\u7528\u4e86\uff0c\u5b83\u5c31\u4ece\u5b57\u5178\u4e2d\u88ab\u79fb\u9664\u4e86\u3002\u89c2\u5bdf\u4e0b\u4e0b\u9762\u7684\u6d4b\u8bd5\u7ed3\u679c\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = get_spam('foo')\nb = get_spam('bar')\nc = get_spam('foo')\nlist(_spam_cache)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "del a\ndel c\nlist(_spam_cache)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "del b\nlist(_spam_cache)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u5bf9\u4e8e\u5927\u90e8\u5206\u7a0b\u5e8f\u800c\u5df2\uff0c\u8fd9\u91cc\u4ee3\u7801\u5df2\u7ecf\u591f\u7528\u4e86\u3002\u4e0d\u8fc7\u8fd8\u662f\u6709\u4e00\u4e9b\u66f4\u9ad8\u7ea7\u7684\u5b9e\u73b0\u503c\u5f97\u4e86\u89e3\u4e0b\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u9996\u5148\u662f\u8fd9\u91cc\u4f7f\u7528\u5230\u4e86\u4e00\u4e2a\u5168\u5c40\u53d8\u91cf\uff0c\u5e76\u4e14\u5de5\u5382\u51fd\u6570\u8ddf\u7c7b\u653e\u5728\u4e00\u5757\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u5c06\u7f13\u5b58\u4ee3\u7801\u653e\u5230\u4e00\u4e2a\u5355\u72ec\u7684\u7f13\u5b58\u7ba1\u7406\u5668\u4e2d\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import weakref\n\nclass CachedSpamManager:\n    def __init__(self):\n        self._cache = weakref.WeakValueDictionary()\n\n    def get_spam(self, name):\n        if name not in self._cache:\n            s = Spam(name)\n            self._cache[name] = s\n        else:\n            s = self._cache[name]\n        return s\n\n    def clear(self):\n            self._cache.clear()\n\nclass Spam:\n    manager = CachedSpamManager()\n    def __init__(self, name):\n        self.name = name\n\n    def get_spam(name):\n        return Spam.manager.get_spam(name)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd9\u6837\u7684\u8bdd\u4ee3\u7801\u66f4\u6e05\u6670\uff0c\u5e76\u4e14\u4e5f\u66f4\u7075\u6d3b\uff0c\u6211\u4eec\u53ef\u4ee5\u589e\u52a0\u66f4\u591a\u7684\u7f13\u5b58\u7ba1\u7406\u673a\u5236\uff0c\u53ea\u9700\u8981\u66ff\u4ee3manager\u5373\u53ef\u3002"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u8fd8\u6709\u4e00\u70b9\u5c31\u662f\uff0c\u6211\u4eec\u66b4\u9732\u4e86\u7c7b\u7684\u5b9e\u4f8b\u5316\u7ed9\u7528\u6237\uff0c\u7528\u6237\u5f88\u5bb9\u6613\u53bb\u76f4\u63a5\u5b9e\u4f8b\u5316\u8fd9\u4e2a\u7c7b\uff0c\u800c\u4e0d\u662f\u4f7f\u7528\u5de5\u5382\u65b9\u6cd5\uff0c\u5982\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "a = Spam('foo')\nb = Spam('foo')\na is b"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6709\u51e0\u79cd\u65b9\u5f0f\u53ef\u4ee5\u9632\u6b62\u7528\u6237\u8fd9\u6837\u505a\uff0c\u7b2c\u4e00\u4e2a\u662f\u5c06\u7c7b\u7684\u540d\u5b57\u4fee\u6539\u4e3a\u4ee5\u4e0b\u5212\u7ebf(_)\u5f00\u5934\uff0c\u63d0\u793a\u7528\u6237\u522b\u76f4\u63a5\u8c03\u7528\u5b83\u3002\n\u7b2c\u4e8c\u79cd\u5c31\u662f\u8ba9\u8fd9\u4e2a\u7c7b\u7684 __init__() \u65b9\u6cd5\u629b\u51fa\u4e00\u4e2a\u5f02\u5e38\uff0c\u8ba9\u5b83\u4e0d\u80fd\u88ab\u521d\u59cb\u5316\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "class Spam:\n    def __init__(self, *args, **kwargs):\n        raise RuntimeError(\"Can't instantiate directly\")\n\n    # Alternate constructor\n    @classmethod\n    def _new(cls, name):\n        self = cls.__new__(cls)\n        self.name = name"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u7136\u540e\u4fee\u6539\u7f13\u5b58\u7ba1\u7406\u5668\u4ee3\u7801\uff0c\u4f7f\u7528 Spam._new() \u6765\u521b\u5efa\u5b9e\u4f8b\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u8c03\u7528 Spam() \u6784\u9020\u51fd\u6570\uff1a"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "# ------------------------\u6700\u540e\u7684\u4fee\u6b63\u65b9\u6848------------------------\nclass CachedSpamManager2:\n    def __init__(self):\n        self._cache = weakref.WeakValueDictionary()\n\n    def get_spam(self, name):\n        if name not in self._cache:\n            temp = Spam3._new(name)  # Modified creation\n            self._cache[name] = temp\n        else:\n            temp = self._cache[name]\n        return temp\n\n    def clear(self):\n            self._cache.clear()\n\nclass Spam3:\n    def __init__(self, *args, **kwargs):\n        raise RuntimeError(\"Can't instantiate directly\")\n\n    # Alternate constructor\n    @classmethod\n    def _new(cls, name):\n        self = cls.__new__(cls)\n        self.name = name\n        return self"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\u6700\u540e\u8fd9\u6837\u7684\u65b9\u6848\u5c31\u5df2\u7ecf\u8db3\u591f\u597d\u4e86\u3002\n\u7f13\u5b58\u548c\u5176\u4ed6\u6784\u9020\u6a21\u5f0f\u8fd8\u53ef\u4ee5\u4f7f\u75289.13\u5c0f\u8282\u4e2d\u7684\u5143\u7c7b\u5b9e\u73b0\u7684\u66f4\u4f18\u96c5\u4e00\u70b9(\u4f7f\u7528\u4e86\u66f4\u9ad8\u7ea7\u7684\u6280\u672f)\u3002"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.1"
    },
    "toc": {
      "base_numbering": 1,
      "nav_menu": {},
      "number_sections": true,
      "sideBar": true,
      "skip_h1_title": true,
      "title_cell": "Table of Contents",
      "title_sidebar": "Contents",
      "toc_cell": false,
      "toc_position": {},
      "toc_section_display": true,
      "toc_window_display": true
    }
  },
  "nbformat": 4,
  "nbformat_minor": 2
}